package com.springboot.dubbo.config;

import com.alibaba.dubbo.config.ServiceConfig;
import com.springboot.config.ProviderConfig;
import com.springboot.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 用户dubbo提供者配置
 *
 * @author supu
 * @create 2019-01-04 18:02
 **/
@Configuration
public class UserDubboProviderConfig {
    @Autowired
    private DubboConfigFactory dubboConfigFactory;

    @Autowired
    private ProviderConfig providerConfig;
    @Autowired
    private UserService userService;

    @Bean
    public ServiceConfig<UserService> userService() {
       return getServiceConfig(UserService.class, userService);
    }

    private <T> ServiceConfig<T> getServiceConfig(Class<T> clz, T ref){
        ServiceConfig<T> serviceConfig = new ServiceConfig<>();
        serviceConfig.setInterface(clz);
        serviceConfig.setGroup(providerConfig.getString(ProviderConfig.DUBBO_GROUP));
        serviceConfig.setTimeout(Integer.parseInt(providerConfig.getString(ProviderConfig.DUBBO_TIMEOUT)));
        serviceConfig.setApplication(dubboConfigFactory.providerApplicationConfig());
        serviceConfig.setProtocol(dubboConfigFactory.protocolConfig());
        serviceConfig.setRegistry(dubboConfigFactory.registryConfig());
        serviceConfig.setRef(ref);
        serviceConfig.export();
        return serviceConfig;
    }
}
